<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>教学班管理</title>
    <!-- Bootstrap Styles-->
    <link href="../../css/bootstrap/bootstrap.css" rel="stylesheet"/>
    <!-- FontAwesome Styles-->
    <link href="../../css/font-awesome.css" rel="stylesheet"/>
    <!-- Morris Chart Styles-->

    <!-- Custom Styles-->
    <link href="../../css/custom-styles.css" rel="stylesheet"/>
    <!-- Google Fonts-->
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'/>
    <!-- TABLE STYLES-->
    <link href="../../js/dataTables/dataTables.bootstrap.css" rel="stylesheet"/>
</head>

<body>
<div id="wrapper">
    <nav class="navbar navbar-default top-navbar" role="navigation">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="tea_course_index.html">题库与考试</a>
        </div>

        <ul class="nav navbar-top-links navbar-right">

            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">
                    <i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
                </a>
                <ul class="dropdown-menu dropdown-user">
                    <li>
                        <a href="../student/stu_person_index.html"><i class="fa fa-user fa-fw"></i> 个人中心</a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="../../login.html"><i class="fa fa-sign-out fa-fw"></i> 退出登录</a>
                    </li>
                </ul>
                <!-- /.dropdown-user -->
            </li>
            <!-- /.dropdown -->
        </ul>

    </nav>
    <!--/. NAV TOP  -->
    <nav class="navbar-default navbar-side" role="navigation">
        <div class="sidebar-collapse">
            <ul class="nav" id="main-menu">
                <li>
                    <a href="tea_course_index.html"><i class="fa fa-dashboard"></i> 课程管理</a>
                </li>
                <li>
                    <a class="active-menu" href="tea_class_index.html"><i class="fa fa-desktop"></i> 教学班管理</a>
                </li>
                <li>
                    <a href="tea_kpoint_index.html"><i class="fa fa-bar-chart-o"></i> 知识点管理</a>
                </li>
                <li>
                    <a href="#"><i class="fa fa-qrcode"></i> 题库管理<span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li>
                            <a href="tea_itbank_new_index.html">新建题库</a>
                        </li>
                        <li>
                            <a href="tea_itbank_defend_index.html">维护题库</a>
                        </li>
                        <li>
                            <a href="tea_itbank_browse_index.html">查看题库</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#"><i class="fa fa-sitemap"></i> 试卷管理<span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li>
                            <a href="tea_paper_new_index.html">创建试卷</a>
                        </li>
                        <li>
                            <a href="tea_paper_browse_index.html">查看试卷列表</a>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#"><i class="fa fa-fw fa-file"></i> 考试管理<span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li>
                            <a href="tea_test_new_index.html">创建考试</a>
                        </li>
                        <li>
                            <a href="tea_test_issue_index.html">发布考试</a>
                        </li>
                        <li>
                            <a href="tea_test_alter_index.html">修改考试设置</a>
                        </li>
                        <li>
                            <a href="tea_test_browse_index.html">查看考试情况</a>
                        </li>
                    </ul>
                </li>
            </ul>

        </div>

    </nav>
    <!-- /. NAV SIDE  -->
    <div id="page-wrapper">
        <div id="page-inner">
            <div class="row">
                <div class="col-md-12">
                    <h1 class="page-header">
                        课程管理
                        <small>我教的课</small>
                        <a href="#mymodal_3" role="button" data-toggle="modal">
                            <div style="display: inline;float: right;">
                                <button type="button" class="btn btn-primary">+添加课程</button>
                            </div>
                        </a>

                    </h1>
                </div>
            </div>
            <!-- /. ROW  -->

            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            课程列表
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                    <tr>
                                        <th>课程名称</th>
                                        <th>课程创建者</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>
            <!-- /. ROW  -->
        </div>
        <!-- /. PAGE INNER  -->
    </div>
    <!-- 模态框1  -->

    <div class="modal fade" id="mymodal_1" tabindex="-1" role="dialog" aria-labelledby="myModelLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <form class="modal-content" id="classForm2">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModal_1Label">
                        修改信息
                    </h4>
                </div>
                <div class="modal-body">
                    <label for="nickname" class="control-label">课程名称</label>
                    <input type="text" class="form-control" id="nickname"/>
                    <label for="produceTeacher" class="control-label">课程表创建者</label>
                    <input type="text" id="produceTeacher" class="form-control" disabled/>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="button" class="btn btn-primary" id="updateCourse">
                        提交更改
                    </button>
                </div>
            </form>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal -->
    </div>
    <!--删除课程-->
    <div class="modal fade" id="mymodal_2" tabindex="-1" role="dialog" aria-labelledby="myModelLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <form class="modal-content" id="courseForm3" method="get">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModal_2Label">
                        是否删除?
                    </h4>
                </div>
                <div class="modal-body">

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="button" class="btn btn-danger" id="deleteCourseBtn">
                        删除
                    </button>
                </div>
            </form>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal -->
    </div>
    <!-- 模态框3 -->
    <!--增加课程-->
    <div class="modal fade" id="mymodal_3" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <form class="modal-content" id="courseForm1" method="post">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        添加课程
                    </h4>
                </div>
                <div class="modal-body">
                    <label for="nickname" class="col-sm-2 control-label">课程名称</label>
                    <div class="col-sm-10">
                        <label>
                            <input type="text" class="form-control" id="coursName" placeholder="输入课程名称"/>
                        </label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="submit" class="btn btn-primary" id="addCourse">
                        确认添加
                    </button>
                </div>
            </form>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal -->
    </div>
    <!-- 使用ajax实现课程的添加 -->

    <!-- /. PAGE WRAPPER  -->
    <!-- /. WRAPPER  -->
    <!-- JS Scripts-->
    <!-- jQuery Js -->
    <script src="../../js/jquery-1.10.2.js"></script>
    <!-- Bootstrap Js -->
    <script src="../../js/bootstrap.min.js"></script>
    <!-- Metis Menu Js -->
    <script src="../../js/jquery.metisMenu.js"></script>
    <!-- DATA TABLE SCRIPTS -->
    <script src="../../js/dataTables/jquery.dataTables.js"></script>
    <script src="../../js/dataTables/dataTables.bootstrap.js"></script>
    <script src="../../js/jquerysession.js"></script>
    <script>
        var courseId;
        $(document).ready(function () {
            var userid = $.session.get('userid');
            var usertype = $.session.get('usertype');
            console.log(userid);
            // console.log(usertype);
            //判断用户是否登录，以及用户的类型
            if (userid == null) {
                alert("请先登录系统");
                window.location.href = "/login.html";
            } else if (userid != null) {
                //用户类型不是教师
                if (usertype != 1) {
                    alert("您没有访问该页面的权限");
                    window.location.href = "/login.html"
                } else {
                    var teachName;
                    $.ajax({
                        type: 'get',
                        url: '/Course/findByCreatorid/' + userid,
                        dataType: 'json',
                        // table数据内容，课程名称，课程创建者，教学班名称，操作
                        success: function (data) {
                            console.log(data);
                            $.each(data.data, function (index, obj) {
                                //查询老师的名字
                                $.ajax({
                                    type: 'get',
                                    url: '/user/findByUserId/' + userid,
                                    dataType: 'json',
                                    success: function (user) {
                                        teachName = user.data.realname;
                                        console.log(teachName);
                                        $("#dataTables-example").append('<tr>' +
                                            '<td>' + obj.name + '</td>' +
                                            '<td>' + teachName + '</td>' +
                                            '<td><button data-target="#mymodal_1" class="btn btn-primary" data-toggle="modal" onclick="updateCourse(' + obj.id + ')">修改</button>' +
                                            '<button data-target="#mymodal_2" class="btn btn-danger" data-toggle="modal" onclick="CourseId(' + obj.id + ')">删除</button>' +
                                            '</tr>');
                                    }
                                });
                            });
                        }
                    });
                }
            }
        });

        $("#deleteCourseBtn").click(function (e) {
           e.preventDefault();
           $.ajax({
               type:'get',
               url:'/Course/delete/'+courseId,
               dataType:'json',
               success:function (data) {
                   if (data.msg == "success"){
                       alert("删除成功");
                   }else{
                       alert(data.msg);
                   }
                   window.location.href="tea_course_index.html";
               }
           })
        });
        
        $("#addCourse").click(function (e) {
           e.preventDefault();
           var userid = $.session.get('userid');
            var courseName = $("#coursName").val();
            if (courseName == ""){
                alert("请填入必要的信息");
            }else{
                var course = JSON.stringify({"creatorid":userid,"maintainerid":userid,"name":courseName});
                console.log(course);
                $.ajax({
                    type:'post',
                    url:'/Course/add',
                    data:course,
                    contentType:'application/json',
                    dataType:'json',
                    success:function (data) {
                        if (data.msg=="success"){
                            alert("增加课程成功");
                        }else{
                            alert(data.msg);
                        }
                        window.location.href="tea_course_index.html";
                    }
                })
            }
        });

        $("#updateCourse").click(function (e) {
            e.preventDefault();
            var courseName = $("#nickname").val();
            var course = JSON.stringify({"id":courseId,"name":courseName});
            $.ajax({
                type:'post',
                url:'/Course/update',
                contentType:"application/json",
                dataType:'json',
                success:function (data) {
                    if (data.msg=="success"){
                        alert("修改成功");
                    }else{
                        alert(data.msg);
                    }
                    window.location.href="tea_course_index.html";
                }
            })
        });

        function updateCourse(id){
            courseId = id;
            var userid = $.session.get("userid");
            $.ajax({
                type:'get',
                url:'/Course/findById/'+id,
                dataType:'json',
                success:function (data) {
                    $("#nickname").val(data.data.name);
                    $.ajax({
                        type:'get',
                        url:'/user/findByUserId/'+userid,
                        dataType:'json',
                        success:function (data2) {
                            $("#produceTeacher").val(data2.data.realname);
                        }
                    })

                }
            })
        }
        function CourseId(ID){
            courseId = ID;
        }
    </script>
</div>
</body>
</html>